package com.changeover.gwt.client.controller;

import com.changeover.gwt.client.event.IEasyEventHandler;
import com.changeover.gwt.client.event.LoginEvent;
import com.changeover.gwt.client.event.LogoutEvent;
import com.changeover.gwt.client.view.impl.CustomerDetailsView;
import com.changeover.gwt.client.view.impl.LoginView;
import com.changeover.gwt.client.view.impl.MainView;
import com.changeover.gwt.shared.model.customer.ICustomer;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.ui.HasWidgets;

/**
 * The class serves for navigation between pages<br>
 * 
 * @author Roman Kostenko
 */
public class ViewsController implements Controller {

    /**
     * Enumerator of type of application pages
     */
    private enum GoToPageType {
        LOGIN_VIEW, MAIN_VIEW, CUSTOMER_DETAILS_VIEW;
    }

    private final HandlerManager eventBus;
    private HasWidgets container;

    @Override
    public void go(HasWidgets container) {
        this.container = container;
        managePresenter(GoToPageType.LOGIN_VIEW);
    }

    public ViewsController(HandlerManager eventBus) {
        this.eventBus = eventBus;
        bind();
    }

    /**
     * Register all the handlers
     */
    private void bind() {

        // Go to main page
        eventBus.addHandler(LoginEvent.TYPE, new IEasyEventHandler() {
            @Override
            public void onProcess() {
                managePresenter(GoToPageType.MAIN_VIEW);
            }
        });
        
        eventBus.addHandler(LogoutEvent.TYPE, new IEasyEventHandler() {
            @Override
            public void onProcess() {
                managePresenter(GoToPageType.LOGIN_VIEW);
            }
        });
//        
//        eventBus.addHandler(CustomerDetailsEvent.TYPE, new ICustomerDetailsEventHandler() {
//            
//            @Override
//            public void onDoubleClickTable(CustomerDetailsEvent event, ICustomer customer) {
//                managePresenter(GoToPageType.CUSTOMER_DETAILS_VIEW, customer);
//            }
//        });
//        
    }
    
    private void managePresenter(GoToPageType actionType) {
        managePresenter(actionType, null);
    }

    private void managePresenter(GoToPageType actionType, Object param) {
        if (actionType != null) {
            Controller presenter = null;
            switch (actionType) {
            case LOGIN_VIEW:
                presenter = new LoginController(eventBus, new LoginView());
                break;
            case MAIN_VIEW:
                presenter = new MainController(eventBus, new MainView());
                break;
            case CUSTOMER_DETAILS_VIEW:
                presenter = new CustomerDetailsController(eventBus, new CustomerDetailsView((ICustomer)param));
                break;
            }
            if (presenter != null) {
                presenter.go(container);
            }
        }
    }

}
